"""统一响应结果."""

from typing import Annotated, Literal

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel


class OkResult[T](BaseModel):
    """Api 统一成功响应结果."""

    model_config = ConfigDict(alias_generator=to_camel)

    success: Annotated[Literal[True], Field(description="成功状态")] = True
    data: Annotated[T, Field(description="数据")]


class FailResult(BaseModel):
    """Api 统一失败响应结果."""

    model_config = ConfigDict(alias_generator=to_camel)

    success: Annotated[Literal[False], Field(description="失败状态")] = False
    exc_kind: Annotated[str, Field(description="异常类型")]
    exc_message: Annotated[str, Field(description="异常信息")]


type Result[T] = OkResult[T] | FailResult
"""Api 统一响应结果."""


def ok[T](data: T) -> OkResult[T]:
    """成功响应."""
    return OkResult(data=data)


def fail(exc_kind: str, exc_message: str) -> FailResult:
    """失败响应."""
    return FailResult.model_construct(exc_kind=exc_kind, exc_message=exc_message)
